home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 051-075 / 069 / mouseclock / mouseclock.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  9KB  |  333 lines

  1. /*
  2.  * MouseClock - Turn your mouse pointer into a digital clock which
  3.  *              updates once a minute.  
  4.  *
  5.  * NOTE: This program directly modifies the preferences data structure.
  6.  *       Using this program while trying to modify your preferences may
  7.  *       have bad results.
  8.  *
  9.  * Copyright (c) 1986, 1987 John R. Hoffman
  10.  *
  11.  * To use MouseClock, just Run it from your Startup-Sequence.  To stop
  12.  * it just delete the window and the previous pointer will be restored.
  13.  *
  14.  * This program is placed in the public domain as long as the above
  15.  * copyright is included.  Sale of this program except for REASONABLE 
  16.  * media costs is prohibited.
  17.  *
  18.  * To create with MANX: cc MouseClock.c
  19.  *                      ln MouseClock.o -lc
  20.  *
  21.  * Error codes:
  22.  *
  23.  *      0 - Normal exit.
  24.  *    101 - Can't get intuition.
  25.  *    102 - Can't create a timer port.
  26.  *    103 - Can't open a timer device.
  27.  *    104 - Can't open a window.
  28.  *
  29.  * Modification History:
  30.  *
  31.  *   11/03/86 John Hoffman - Original version.
  32.  *   01/02/87 John Hoffman - Added call to set task priority to -5,
  33.  *                           fixed hot point location,
  34.  *                           fixed to work with 16 bit integers,
  35.  *                           added code to adjust update time to 
  36.  *                           five seconds after the minute.
  37.  *
  38.  */
  39.  
  40. static char *Copyright = "Copyright (c) 1986, 1987 John R. Hoffman";
  41.  
  42. /* #define PRINT_ERRORS */
  43.  
  44. #ifdef PRINT_ERRORS
  45. #include <stdio.h>
  46. #endif
  47.  
  48. #include <exec/types.h>
  49. #include <libraries/dos.h>
  50. #include <intuition/intuitionbase.h>
  51. #include <intuition/intuition.h>
  52.  
  53.     /* The pointer image. */
  54.  
  55. static USHORT Clock[] = {
  56.     0x0000, 0x0000, 0xf800, 0xf800, 0xe000, 0xe000, 0xb000, 0xb000,
  57.     0x9800, 0x9800, 0x0c00, 0x0c00, 0xfffe, 0xfffe, 0x0000, 0xfffe,
  58.     0xb8ee, 0xfffe, 0xa8aa, 0xfffe, 0xaaaa, 0xfffe, 0xb8ee, 0xfffe,
  59.     0xaaaa, 0xfffe, 0xa8aa, 0xfffe, 0xb8ee, 0xfffe, 0x0000, 0xfffe,
  60.     0xfffe, 0xfffe, 0x0000, 0x0000
  61. };
  62.  
  63.     /* The hot point offset for the pointer. */
  64.  
  65. #define CLOCK_HOT_X -1
  66. #define CLOCK_HOT_Y  0
  67.  
  68.     /* The positions in the pointer image which are replaced. */
  69.  
  70. static USHORT positions[] = { 16, 18, 20, 22, 24, 26, 28 };
  71.  
  72.     /* The number of positions. */
  73.  
  74. #define NUMPOS (sizeof(positions)/sizeof(USHORT))
  75.  
  76.     /* The bitmap for each of the digits. */
  77.  
  78. static USHORT digits[10][7] = {
  79.     0x7, 0x5, 0x5, 0x5, 0x5, 0x5, 0x7,    /* 0 */
  80.     0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2,    /* 1 */
  81.     0x7, 0x1, 0x1, 0x7, 0x4, 0x4, 0x7,    /* 2 */
  82.     0x7, 0x1, 0x1, 0x7, 0x1, 0x1, 0x7,    /* 3 */
  83.     0x5, 0x5, 0x5, 0x7, 0x1, 0x1, 0x1,    /* 4 */
  84.     0x7, 0x4, 0x4, 0x7, 0x1, 0x1, 0x7,    /* 5 */
  85.     0x7, 0x4, 0x4, 0x7, 0x5, 0x5, 0x7,    /* 6 */
  86.     0x7, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,    /* 7 */
  87.     0x7, 0x5, 0x5, 0x7, 0x5, 0x5, 0x7,    /* 8 */
  88.     0x7, 0x5, 0x5, 0x7, 0x1, 0x1, 0x7     /* 9 */
  89. };
  90.  
  91. static struct NewWindow New_Window = {
  92.     426, 0, 164, 10,
  93.     -1, -1,
  94.     CLOSEWINDOW,
  95.     WINDOWCLOSE | WINDOWDEPTH | WINDOWDRAG | SIMPLE_REFRESH | NOCAREREFRESH,
  96.     NULL, NULL,
  97.     (UBYTE *) "MouseClock ",
  98.     NULL, NULL, 0, 0, 0, 0,
  99.     WBENCHSCREEN
  100.     };
  101.  
  102. struct IntuitionBase *IntuitionBase = NULL;
  103. extern struct IntuitionBase *OpenLibrary();
  104.  
  105. static struct Window *Window = NULL;
  106. extern struct Window *OpenWindow();
  107.  
  108. static struct MsgPort *Msg_Port = NULL;
  109. extern struct MsgPort *CreatePort();
  110.  
  111. static struct timerequest Timer_Request;
  112.  
  113. extern struct Task *FindTask();
  114. extern struct IntuiMessage *GetMsg();
  115.  
  116. USHORT SetTime();
  117.  
  118. main()
  119. {
  120.     BYTE old_hotX, old_hotY;
  121.     USHORT second, old_pointer[POINTERSIZE];
  122.     struct Preferences pref;
  123.     struct IntuiMessage *Msg;
  124.  
  125.     SetTaskPri(FindTask(0L), -5L);
  126.  
  127.     if ( (IntuitionBase = OpenLibrary("intuition.library", 0L)) == NULL )
  128.         finish(101, "Can't get intuition.\n");
  129.  
  130.     if ( (Msg_Port = CreatePort("timer.port", 0L)) == NULL )
  131.         finish(102, "Can't create a timer port.\n");
  132.  
  133.     if ( OpenDevice(TIMERNAME, (long) UNIT_VBLANK, &Timer_Request, 0L) != 0 )
  134.         finish(103, "Can't open a timer device.\n");
  135.  
  136.     Timer_Request.tr_node.io_Message.mn_ReplyPort    = Msg_Port;
  137.     Timer_Request.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  138.     Timer_Request.tr_node.io_Message.mn_Node.ln_Pri  = 0;
  139.  
  140.     Timer_Request.tr_node.io_Command = TR_ADDREQUEST;
  141.  
  142.     if ( (Window = (struct Window *) OpenWindow(&New_Window)) == NULL )
  143.         finish(104, "Can't open a window.\n");
  144.  
  145.     GetMouse(&old_pointer, &old_hotX, &old_hotY);
  146.  
  147.     second = SetTime(&Clock, NUMPOS, &positions);
  148.  
  149.     SetMouse(&Clock, CLOCK_HOT_X, CLOCK_HOT_Y);
  150.  
  151.     for ( ; ; ) {
  152.  
  153.         Timer_Request.tr_time.tv_secs  = 60 - second + 5;
  154.         Timer_Request.tr_time.tv_micro = 0;
  155.  
  156.         SendIO(&Timer_Request.tr_node);
  157.  
  158.         Wait(1L << Window->UserPort->mp_SigBit | 
  159.              1L << Msg_Port->mp_SigBit);
  160.  
  161.         while ( (Msg = GetMsg(Window->UserPort)) != 0 ) {
  162.             if ( Msg->Class == CLOSEWINDOW ) {
  163.                 ReplyMsg(Msg);
  164.                 SetMouse(&old_pointer, old_hotX, old_hotY);
  165.                 finish(0, "");
  166.             }
  167.             ReplyMsg(Msg);
  168.         }
  169.  
  170.         GetMsg(Msg_Port);
  171.  
  172.         second = SetTime(&Clock, NUMPOS, &positions);
  173.  
  174.         SetClock(&Clock, NUMPOS, &positions);
  175.  
  176.     }
  177. }
  178.  
  179. finish(code, msg)
  180. int code;
  181. char *msg;
  182. {
  183.     AbortIO(&Timer_Request.tr_node);
  184.  
  185.     if ( Window != NULL )
  186.         CloseWindow(Window);
  187.  
  188.     if ( Timer_Request.tr_node.io_Message.mn_ReplyPort != NULL )
  189.         CloseDevice(&Timer_Request);
  190.  
  191.     if ( Msg_Port != NULL )
  192.         DeletePort(Msg_Port);
  193.  
  194.     if ( IntuitionBase != NULL )
  195.         CloseLibrary(IntuitionBase);
  196.  
  197. #ifdef PRINT_ERRORS
  198.     if ( msg[0] != '\0' )
  199.         fputs(msg, stderr);
  200. #endif
  201.  
  202.     exit(code);
  203. }
  204.  
  205. /* Get the whole pointer matrix and the hot point offset. */
  206.  
  207. GetMouse(pointer, hotX, hotY)
  208. register USHORT *pointer;
  209. BYTE *hotX;
  210. BYTE *hotY;
  211. {
  212.     register int i;
  213.     struct Preferences pref;
  214.  
  215.     Forbid();
  216.  
  217.         GetPrefs(&pref, (long)sizeof(struct Preferences));
  218.  
  219.             for ( i = 0; i < POINTERSIZE; i++ )
  220.                 pointer[i] = pref.PointerMatrix[i];
  221.  
  222.             *hotX = pref.XOffset;
  223.             *hotY = pref.YOffset;
  224.  
  225.     Permit();
  226. }
  227.  
  228. /* Set the current time in the pointer matrix. */
  229.  
  230. USHORT SetTime(pointer, numpos, pos)
  231. register USHORT *pointer;
  232. register USHORT numpos;
  233. register USHORT *pos;
  234. {
  235.     register int i;
  236.     struct DateStamp tnow;
  237.     USHORT hour10, hour1, min10, min1;
  238.     static USHORT hour10_old = 99;
  239.     static USHORT hour1_old = 99;
  240.     static USHORT min10_old = 99;
  241.  
  242.     DateStamp(&tnow);
  243.  
  244.     hour1  = (tnow.ds_Minute / 60) % 12;
  245.     hour1  = (hour1 == 0) ? 12 : hour1;
  246.     hour10 = hour1 / 10;
  247.     hour1  = hour1 % 10;
  248.  
  249.     min1  = tnow.ds_Minute % 60;
  250.     min10 = min1 / 10;
  251.     min1  = min1 % 10;
  252.  
  253.     if ( hour10_old != hour10 ) {
  254.         for ( i=0; i < numpos; i++ ) {
  255.             if ( hour10 == 1 )
  256.                 pointer[pos[i]] |= 0x8000;
  257.             else
  258.                 pointer[pos[i]] &= 0x7fff;
  259.         }
  260.         hour10_old = hour10;
  261.     }
  262.  
  263.     if ( hour1_old != hour1 ) {
  264.         for ( i=0; i < numpos; i++ ) {
  265.             pointer[pos[i]] &= 0xc7ff;
  266.             pointer[pos[i]] |= (digits[hour1][i] << 11);
  267.         }
  268.         hour1_old = hour1;
  269.     }
  270.  
  271.     if ( min10_old != min10 ) {
  272.         for ( i=0; i < numpos; i++ ) {
  273.             pointer[pos[i]] &= 0xff1f;
  274.             pointer[pos[i]] |= (digits[min10][i] << 5);
  275.         }
  276.         min10_old = min10;
  277.     }
  278.  
  279.     for ( i=0; i < numpos; i++ ) {
  280.         pointer[pos[i]] &= 0xfff1;
  281.         pointer[pos[i]] |= (digits[min1][i] << 1);
  282.     }
  283.  
  284.     return((USHORT)(tnow.ds_Tick/TICKS_PER_SECOND));
  285. }
  286.  
  287. /* Set certain positions in the pointer matrix. */
  288.  
  289. SetClock(pointer, numpos, pos)
  290. register USHORT *pointer;
  291. register USHORT numpos;
  292. register USHORT *pos;
  293. {
  294.     register int i;
  295.     struct Preferences pref;
  296.  
  297.     Forbid();
  298.  
  299.         GetPrefs(&pref, (long)sizeof(struct Preferences));
  300.  
  301.         for ( i = 0; i < numpos; i++ )
  302.             pref.PointerMatrix[pos[i]] = pointer[pos[i]];
  303.  
  304.         SetPrefs(&pref, (long)sizeof(struct Preferences), FALSE);
  305.  
  306.     Permit();
  307. }
  308.  
  309. /* Set the whole pointer matrix and the hot point offset. */
  310.  
  311. SetMouse(pointer, hotX, hotY)
  312. register USHORT *pointer;
  313. BYTE hotX;
  314. BYTE hotY;
  315. {
  316.     register int i;
  317.     struct Preferences pref;
  318.  
  319.     Forbid();
  320.  
  321.         GetPrefs(&pref, (long)sizeof(struct Preferences));
  322.  
  323.             for ( i = 0; i < POINTERSIZE; i++ )
  324.                 pref.PointerMatrix[i] = pointer[i];
  325.  
  326.             pref.XOffset = hotX;
  327.             pref.YOffset = hotY;
  328.  
  329.         SetPrefs(&pref, (long)sizeof(struct Preferences), FALSE);
  330.  
  331.     Permit();
  332. }
  333.